package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("UserController")
public class UserController extends CmiEmaController {

	@RequestMapping("/listUsers")
	public ModelAndView listUsers(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = userPaginationService.updatePagination(request, pagination);
		Set<User> users = userPaginationService.getPaginationUsers(pagination);

		mav.addObject("users", users);
		mav.addObject("entityName", "user");
		mav.addObject("pagination", pagination);
		mav.setViewName("user/listUsers.jsp");
		return mav;
	}

	public String listUsers() {
		return "redirect:/listUsers";
	}

	@RequestMapping("/viewUser")
	public ModelAndView viewUser(@RequestParam Integer userId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("user", userDAO.findUserByPrimaryKey(userId));
		mav.setViewName("user/viewUser.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteUsers")
	public ModelAndView confirmDeleteUsers(
			@RequestParam(value = "deleteUsersIds", required = true) Set<Integer> deleteUsersIds) {
		ModelAndView mav = new ModelAndView();

		Set<User> deleteUsers = userDAO.findUsersByIds(deleteUsersIds);
		Set<User> referencedUsers = userService.loadReferenceUsers(deleteUsers);
		boolean hasReferenced;
		if (referencedUsers.size() == 0) {
			mav.addObject("deleteEntitys", deleteUsers);
			hasReferenced = false;
		} else {
			deleteUsers.removeAll(referencedUsers);
			mav.addObject("referencedEntitys", referencedUsers);
			mav.addObject("deleteEntitys", deleteUsers);
			hasReferenced = true;
		}
		mav.addObject("entityName", "User");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listUsers");
		return mav;
	}

	@RequestMapping("/deleteUser")
	public String deleteUser(@RequestParam Integer userId) {
		User user = userDAO.findUserByPrimaryKey(userId);
		userService.deleteUser(user);
		return "forward:/listUsers";
	}

	@RequestMapping("/confirmDeleteUser")
	public ModelAndView confirmDeleteUser(@RequestParam Integer userId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("user", userDAO.findUserByPrimaryKey(userId));

		mav.setViewName("user/deleteUser.jsp");

		return mav;
	}

	@RequestMapping("/deleteUsers")
	public String deleteUsers(@RequestParam(value = "deleteUsersId", required = true) Set<Integer> deleteUsersId) {
		userService.deleteUsers(deleteUsersId);
		return "forward:/listUsers";
	}

	@RequestMapping("/editUser")
	public ModelAndView editUser(@RequestParam Integer userId) {

		ModelAndView mav = new ModelAndView();

		User user = userDAO.findUserByPrimaryKey(userId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("user", user);

		// 把所有的引用user对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(userService.loadUserObjectReferencedSet(user));

		// 将编辑页面user添加到mav中
		mav.setViewName("user/editUser.jsp");
		return mav;
	}

	@RequestMapping("/newUser")
	public ModelAndView newUser() {
		ModelAndView mav = new ModelAndView();
		User user = new User();

		mav.addObject("user", user);

		// 把所有的引用user对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(userService.loadUserObjectReferencedSet(user));

		mav.addObject("newFlag", true);
		mav.setViewName("user/editUser.jsp");

		return mav;
	}

	@RequestMapping("/saveUser")
	public ModelAndView saveUser(@ModelAttribute User user) {

		ModelAndView mav = new ModelAndView();

		User existingUser = CmiHibernateProxyUtil
				.deproxy(userDAO.findUserByPhysicalPrimaryKey(user.getName(), user.getBirthday(), user.getEmail()));

		// 新建记录
		if (user.getId() == null) {
			if (user.equals(existingUser)) {

				mav.addAllObjects(userService.loadUserClassReferencedSet(user));
				// mav = editContinue(mav, user);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("user/editUser.jsp");
			} else {
				userService.saveUser(user);
				mav.setViewName("forward:/listUsers");
			}
		}
		// 编辑记录
		else {
			User originUser = CmiHibernateProxyUtil.deproxy(userDAO.findUserByPrimaryKey(user.getId()));
			if (!user.equals(originUser) && user.equals(existingUser)) {
				mav.addAllObjects(userService.loadUserClassReferencedSet(user));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("user/editUser.jsp");
			} else {
				userService.saveUser(user);
				mav.setViewName("forward:/listUsers");
			}
		}
		return mav;
	}

	@RequestMapping("/userController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
